
package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

import DBM.DBManager;
import bean.AccountBEAN;

public class AccountDAO {
	private final String	addAccount		= "INSERT INTO Account VALUES(?, ?, ?, ?, ?)";
	private final String	newId			= "SELECT COUNT(*) AS newid FROM Account";
	private final String	checkLogin		= "SELECT password FROM Account WHERE accountID = ?";
	private final String	printAccList	= "SELECT * FROM Account";
	private DBManager		dbmanager		= null;
	private CharacterDAO	characterDAO	= null;
	private ActionDAO		actionDAO		= null;
	private MailDAO			mailDAO			= null;

	public AccountDAO( DBManager DBM, CharacterDAO characterDAO,
			ActionDAO actionDAO, MailDAO mailDAO ) {
		this.dbmanager = DBM;
		this.characterDAO = characterDAO;
		this.actionDAO = actionDAO;
		this.mailDAO = mailDAO;
	}

	public void addAccount( String charName, String realName, String password,
			String mail ) {
		int newID = getNewID();
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( addAccount );
			ppstm.setInt( 1, newID );
			ppstm.setInt( 2, newID );
			ppstm.setString( 3, realName );
			ppstm.setString( 4, password );
			ppstm.setString( 5, mail );
			ppstm.execute();
			characterDAO.addCharacter( newID, charName );
			actionDAO.addAction( newID );
			mailDAO.addMail(
					1,
					newID,
					"Welcome!",
					"Hi there! <br>I welcome you to one of the best crime games on the web. "
							+ "you can start you're journey to become one of the most powerfull persons on this game. "
							+ "<br> If you have questions you can look in the FAQ page. still have questions? mail te support on the support page."
							+ "<br> also if you find bugs and problems mail them to the support! you will get rewarded :) "
							+ "<br> Greetz Kenny (Superadmin)." );
		} catch( SQLIntegrityConstraintViolationException e ) {
			e.printStackTrace();
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	private int getNewID() {
		int newID = 0;
		try {
			PreparedStatement ppstm = dbmanager.makePreparedStatement( newId );
			ResultSet res = ppstm.executeQuery();
			if( res.next() ) {
				newID = res.getInt( "newid" );
			}
		} catch( SQLException e ) {
			e.printStackTrace();
		}
		return newID + 1;
	}

	public void printAccountList() {
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( printAccList );
			ResultSet res = ppstm.executeQuery();
			while( res.next() ) {
				AccountBEAN account = new AccountBEAN(
						res.getInt( "accountID" ),
						characterDAO.getCharacterByID( res
								.getInt( "characterID" ) ), res
								.getString( "realName" ), res
								.getString( "password" ), res
								.getString( "email" ) );
				System.out.println( account.toString() );
			}
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	public boolean checkLogin( int accountID, String password ) {
		boolean check = false;
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( checkLogin );
			ppstm.setInt( 1, accountID );
			ResultSet res = ppstm.executeQuery();
			if( res.next() ) {
				check = ( res.getString( "password" ).equals( password ) );
			}
		} catch( SQLException e ) {
			e.printStackTrace();
		}
		return check;
	}

	public int getAccountByLogin( String loginName ) {
		return( getNewID() - 1 );
	}
}
